<html>
<head>
<title>AddAndEditProductActivity.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }
.s0 { color: rgb(0,0,128); font-weight: bold; }
.s1 { }
.s2 { color: rgb(0,0,255); }
.s3 { color: rgb(0,128,0); font-weight: bold; }
.s4 { color: rgb(128,128,128); font-style: italic; }
.s5 { color: rgb(128,128,128); font-weight: bold; font-style: italic; }
</style>
</head>
<BODY BGCOLOR="#ffffff">
<TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#C0C0C0" >
<TR><TD><CENTER>
<FONT FACE="Arial, Helvetica" COLOR="#000000">
AddAndEditProductActivity.java</FONT>
</center></TD></TR></TABLE>
<pre>

<span class="s0">package </span><span class="s1">com.example.lista_zakupow.activities.products; 
 
</span><span class="s0">import </span><span class="s1">android.app.Activity; 
</span><span class="s0">import </span><span class="s1">android.app.AlertDialog; 
</span><span class="s0">import </span><span class="s1">android.app.Dialog; 
</span><span class="s0">import </span><span class="s1">android.content.DialogInterface; 
</span><span class="s0">import </span><span class="s1">android.content.Intent; 
</span><span class="s0">import </span><span class="s1">android.os.Bundle; 
</span><span class="s0">import </span><span class="s1">android.view.View; 
</span><span class="s0">import </span><span class="s1">android.widget.*; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.R; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.database.dao.ProduktyDao; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.zakupy.Jednostki; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.zakupy.ListaZakupow; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.zakupy.Produkt; 
 
</span><span class="s0">public class </span><span class="s1">AddAndEditProductActivity </span><span class="s0">extends </span><span class="s1">Activity { 
    </span><span class="s0">public static final int </span><span class="s1">NO_PRODUCT_NAME_DIALOG = </span><span class="s2">1</span><span class="s1">; 
 
    </span><span class="s0">private </span><span class="s1">ProduktyDao produktyDao; 
 
    </span><span class="s0">private </span><span class="s1">ArrayAdapter &lt;CharSequence&gt; iloscAdapter; 
    </span><span class="s0">private </span><span class="s1">ArrayAdapter &lt;CharSequence&gt; jednostkiAdapter; 
 
    </span><span class="s0">private </span><span class="s1">Produkt produkt; 
    </span><span class="s0">private </span><span class="s1">ListaZakupow listaZakupow; 
 
    </span><span class="s0">private </span><span class="s1">Spinner iloscSpinner; 
    </span><span class="s0">private </span><span class="s1">Spinner jednostkiSpinner; 
 
    </span><span class="s0">private </span><span class="s1">EditText productNameEditText; 
 
    </span><span class="s0">private </span><span class="s1">Button saveBtn; 
 
    </span><span class="s0">private boolean </span><span class="s1">czyEdit = </span><span class="s0">false</span><span class="s1">; 
 
    @Override 
    </span><span class="s0">protected void </span><span class="s1">onCreate(Bundle savedInstanceState) { 
        </span><span class="s0">super</span><span class="s1">.onCreate(savedInstanceState); 
        setContentView(R.layout.add_and_edit_product); 
 
        produkt = (Produkt) getIntent().getExtras().get(</span><span class="s3">&quot;produkt&quot;</span><span class="s1">); 
        listaZakupow = (ListaZakupow) getIntent().getExtras().get(</span><span class="s3">&quot;lista&quot;</span><span class="s1">); 
 
        produktyDao = </span><span class="s0">new </span><span class="s1">ProduktyDao(getApplicationContext(), listaZakupow); 
 
        productNameEditText = (EditText) findViewById(R.id.productNameEditText); 
 
        </span><span class="s0">if </span><span class="s1">(getIntent().getExtras().get(</span><span class="s3">&quot;czyEdit&quot;</span><span class="s1">).equals(</span><span class="s3">&quot;tak&quot;</span><span class="s1">)) { 
            czyEdit = </span><span class="s0">true</span><span class="s1">; 
            setUpForm(); 
        } 
 
        iloscSpinner = (Spinner) findViewById(R.id.ilosc_spinner); 
        jednostkiSpinner = (Spinner) findViewById(R.id.jednostka_spinner); 
 
        iloscAdapter = ArrayAdapter.createFromResource(</span><span class="s0">this</span><span class="s1">, 
                R.array.ilosc_array, android.R.layout.simple_spinner_item); 
        iloscAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        iloscSpinner.setAdapter(iloscAdapter); 
 
        jednostkiAdapter = ArrayAdapter.createFromResource(</span><span class="s0">this</span><span class="s1">, 
                R.array.jednostki_array, android.R.layout.simple_spinner_item); 
        jednostkiAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        jednostkiSpinner.setAdapter(jednostkiAdapter); 
 
        saveBtn = (Button) findViewById(R.id.buttonSaveProd); 
        saveBtn.setOnClickListener(</span><span class="s0">new </span><span class="s1">OnSaveBtnClickListener()); 
    } 
 
    </span><span class="s4">/** 
     * Przygotowanie formularza 
     */</span><span class="s1"> 
    </span><span class="s0">private void </span><span class="s1">setUpForm() { 
        productNameEditText.setText(produkt.getName()); 
    } 
 
    </span><span class="s4">/** 
     * Słuchacz przycisku zapisz 
     */</span><span class="s1"> 
    </span><span class="s0">private class </span><span class="s1">OnSaveBtnClickListener </span><span class="s0">implements </span><span class="s1">View.OnClickListener { 
        @Override 
        </span><span class="s0">public void </span><span class="s1">onClick(View view) { 
            </span><span class="s0">if </span><span class="s1">(productNameEditText.getText().toString().equals(</span><span class="s3">&quot;&quot;</span><span class="s1">)) { 
                showDialog(NO_PRODUCT_NAME_DIALOG); 
            } </span><span class="s0">else </span><span class="s1">{ 
                </span><span class="s0">if </span><span class="s1">(czyEdit) { 
                    produkt = getProductFromFields(); 
                    produktyDao.update(produkt); 
                    backToPreviousActivity(); 
                } </span><span class="s0">else </span><span class="s1">{ 
                    Produkt produkt1 = getProductFromFields(); 
                    produktyDao.save(produkt1); 
                    backToPreviousActivity(); 
                } 
            } 
        } 
    } 
 
    </span><span class="s4">/** 
     * Powrót do głównej aktywności 
     */</span><span class="s1"> 
    </span><span class="s0">private void </span><span class="s1">backToPreviousActivity() { 
        Intent intent = </span><span class="s0">new </span><span class="s1">Intent(getApplicationContext(), ShowProductsActivity.</span><span class="s0">class</span><span class="s1">); 
        intent.putExtra(</span><span class="s3">&quot;lista&quot;</span><span class="s1">, listaZakupow); 
        startActivity(intent); 
    } 
 
    </span><span class="s4">/** 
     * Okno dialogowe 
     * </span><span class="s5">@param </span><span class="s4">id nr okna dialogowego 
     * </span><span class="s5">@return </span><span class="s4">okno dialogowe 
     */</span><span class="s1"> 
    @Override 
    </span><span class="s0">protected </span><span class="s1">Dialog onCreateDialog(</span><span class="s0">int </span><span class="s1">id) { 
        </span><span class="s0">switch </span><span class="s1">(id) { 
            </span><span class="s0">case </span><span class="s1">NO_PRODUCT_NAME_DIALOG: { 
                </span><span class="s0">return new </span><span class="s1">AlertDialog.Builder(</span><span class="s0">this</span><span class="s1">) 
                        .setMessage(</span><span class="s3">&quot;Nie wpisałeś nazwy&quot;</span><span class="s1">) 
                        .setPositiveButton(</span><span class="s3">&quot;OK&quot;</span><span class="s1">, </span><span class="s0">new </span><span class="s1">DialogInterface.OnClickListener() { 
                            @Override 
                            </span><span class="s0">public void </span><span class="s1">onClick(DialogInterface arg0, </span><span class="s0">int </span><span class="s1">arg1) { 
                            } 
                        }).create(); 
            } 
        } 
        </span><span class="s0">return null</span><span class="s1">; 
    } 
 
    </span><span class="s4">/** 
     *  Metoda pobierająca produkt z formularza 
     * </span><span class="s5">@return </span><span class="s4">produkt wpisany do formularza 
     */</span><span class="s1"> 
    </span><span class="s0">private </span><span class="s1">Produkt getProductFromFields() { 
        </span><span class="s0">if </span><span class="s1">(czyEdit) { 
            produkt.setName(productNameEditText.getText().toString()); 
            produkt.setIlosc(Double.valueOf(String.valueOf(iloscSpinner.getSelectedItem()))); 
            produkt.setJednostka(Jednostki.valueOf(String.valueOf(jednostkiSpinner.getSelectedItem()))); 
            </span><span class="s0">return </span><span class="s1">produkt; 
        } </span><span class="s0">else </span><span class="s1">{ 
            String productName = productNameEditText.getText().toString(); 
            </span><span class="s0">double </span><span class="s1">ilosc = Double.valueOf(String.valueOf(iloscSpinner.getSelectedItem())); 
            String jednostkaString = String.valueOf(jednostkiSpinner.getSelectedItem()); 
            Jednostki jednostka = Jednostki.valueOf(jednostkaString); 
 
            </span><span class="s0">return new </span><span class="s1">Produkt(productName, ilosc, jednostka); 
        } 
 
    } 
} 
</span></pre>
</body>
</html>